package com.example.safe.keyboard;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.PopupWindow;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.safe.keyboard.databinding.ActivityPopupwindowTestBinding;
import com.example.safe.keyboard.databinding.LayoutSingleEditBinding;
import com.surfin.safe.keyboard.SafeKeyboard;

public class PopupWindowTestActivity extends AppCompatActivity {

    public static void start(Context context) {
        context.startActivity(new Intent(context, PopupWindowTestActivity.class));
    }

    private SafeKeyboard safeKeyboard;

    private PopupWindow popupWindow;

    ActivityPopupwindowTestBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPopupwindowTestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitle(R.string.keyboard_popupWindow_test);
        binding.btnShowPopupWindow.setOnClickListener(v -> showPopupWindow());
    }

    private void showPopupWindow() {
        LayoutSingleEditBinding popBinding = LayoutSingleEditBinding.inflate(getLayoutInflater());
        if (binding.cbUseSafeKeyboard.isChecked()) {
            if (safeKeyboard == null) {
                safeKeyboard = new SafeKeyboard(this, popBinding.llContainer, popBinding.ctlRoot);
                // 默认的预览功能是使用 PopupWindow 实现的, 而 PopupWindow 中不能再弹出 PopupWindow, 所以这里需要关闭预览功能,否则报错
                // android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@3068453 is not valid; is your activity running?
                // safeKeyboard.setForbidPreview(false);
            }
        }
        if (popupWindow == null) {
            popupWindow = new PopupWindow(popBinding.getRoot(), Util.getScreenWidth(), Util.getScreenHeight()) {

                @Override
                public void dismiss() {
                    // PopupWindow关闭的时候先关闭键盘
                    if (safeKeyboard != null && safeKeyboard.stillNeedOptManually(false)) {
                        safeKeyboard.hideKeyboard();
                        return;
                    }
                    super.dismiss();
                }
            };
            if (safeKeyboard != null) {
                safeKeyboard.setPopupParent(binding.getRoot());
            }
            popupWindow.setTouchable(true);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0xFFFFFFFF));
            popupWindow.setOnDismissListener(() -> {
                if (safeKeyboard != null && safeKeyboard.stillNeedOptManually(false)) {
                    safeKeyboard.hideKeyboard();
                }
            });
            popupWindow.setOnDismissListener(() -> {
                popupWindow = null;
                if (safeKeyboard != null) {
                    safeKeyboard.release();
                    safeKeyboard = null;
                }
            });
            if (safeKeyboard != null) {
                safeKeyboard.putEditText(popBinding.etText);
            }
        }
        popupWindow.showAsDropDown(binding.btnShowPopupWindow);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (safeKeyboard != null) {
            safeKeyboard.release();
            safeKeyboard = null;
        }
    }
}


